package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.InformationDetailInfo;
import com.lanlian.commonlib.bean.ReqInformationDetail;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/9/17 19:27
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class InformationDetailActivity extends BaseActivity {

    private WebView mWebView;
    private TextView mTvInforTitle;
    private TextView mTvMTitle;
    private TextView mTvTime;

    private int mInformationId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information_detail, true);
        initView();
        initData();
        getInformationDetail();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("资讯详情");
        mTvInforTitle = findViewById(R.id.tv_infor_title);
        mTvMTitle = findViewById(R.id.tv_m_title);
        mTvTime = findViewById(R.id.tv_time);
        mWebView = findViewById(R.id.web_view);
        mWebView = findViewById(R.id.web_view);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());
    }

    @Override
    protected void initData() {
        super.initData();
        mInformationId = getIntent().getIntExtra("id", 0);
    }

    private void getInformationDetail() {
        if (mInformationId == 0) {
            return;
        }
        showLoadingDialog();
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), new ReqInformationDetail(mInformationId),
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        String string = (String) response;
                        try {
                            String s = string.substring(1, string.indexOf("}", 2) + 1);
                            InformationDetailInfo informationDetailInfo = new Gson().fromJson(s, InformationDetailInfo.class);
                            if (informationDetailInfo != null) {
                                mTvInforTitle.setText(informationDetailInfo.getTitle());
                                mTvMTitle.setText("【摘要】" + informationDetailInfo.getDescription());
                                mTvTime.setText(informationDetailInfo.getUpdatetime());
                                mWebView.loadDataWithBaseURL(null, informationDetailInfo.getContent(),
                                        "text/html", "UTF-8", null);
                            }
                        } catch (Exception e) {
                            showToast("数据错误");
                            finish();
                        }
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                    }
                });
    }

    @Override
    protected void onStop() {
        super.onStop();
        mWebView = null;
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            imgReset();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    /**
     * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
     **/
    private void imgReset() {
        if (mWebView != null) {
            mWebView.loadUrl("javascript:(function(){" +
                    "var objs = document.getElementsByTagName('img'); " +
                    "for(var i=0;i<objs.length;i++)  " +
                    "{"
                    + "var img = objs[i];   " +
                    "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
                    "}" +
                    "})()");
        }
    }

}
